package com.qingzhu.controller.goodsserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qingzhu.pojo.CustomPage;
import com.qingzhu.pojo.Goods;
import com.qingzhu.pojo.Item;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.pojovo.SearchResult;
import com.qingzhu.service.GoodsService;
import com.qingzhu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectgoods")
public class GoodsSelectController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("商品查询服务..");
        String serverName = req.getParameter("server");

        if (serverName != null && serverName.equals("findgoodsbyid")){
            findGoodsById(req,resp);
        }else if (serverName != null && serverName.equals("finditem")) {
            findItem(req,resp);
        }else if (serverName != null && serverName.equals("search")) {
            findGoodsByLike(req,resp);
        }else {
            resp.getWriter().write(objectMapper.writeValueAsString(new ResultVo(1001,"服务器类型标记为空")));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("筛选条件查询..");
        // 获取筛选条件
        String screenData = req.getReader().readLine();
        SearchResult searchResult = objectMapper.readValue(screenData, SearchResult.class);
        // 查询
        screenSearch(searchResult,resp);
    }

    // 初始化指定商品(SPU+SKU)
    public void findGoodsById(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id = req.getParameter("brandId");
        Goods goods = goodsService.findGoodsById(Integer.parseInt(id));
        ResultVo resultVo = new ResultVo(200,goods);
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 查找SKU
    public void findItem(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String goodsId = req.getParameter("goodsId");
        String edition = req.getParameter("edition");
        String colour = req.getParameter("colour");
        String network = req.getParameter("network");
        System.out.println("SPU"+goodsId+" 型号:"+edition+" 颜色:"+colour+" 网络:"+network);
        Item item = goodsService.findItem(Integer.parseInt(goodsId), edition, colour, network);
        if (item != null) {
            resp.getWriter().write(objectMapper.writeValueAsString(new ResultVo(200,item)));
        }else {
            resp.getWriter().write(objectMapper.writeValueAsString(new ResultVo(1000,"查找结果为空")));
        }
    }
    // 搜索查询
    public void findGoodsByLike(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String keyword = req.getParameter("keyword");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        System.out.println("关键字："+keyword);
        Page<Goods> page = goodsService.findGoodsByLike(keyword,Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        PageInfo pageInfo = new PageInfo(page);
        System.out.println("分页结果："+pageInfo);
        ResultVo resultVo = page.size() > 0 ?
                new ResultVo(200,pageInfo) : new ResultVo(1000,"仓库中没有您需要的商品呢");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 筛选查询（根据用户选中的品牌和规格进行查找）
    public void screenSearch(SearchResult searchResult, HttpServletResponse resp) throws IOException{
        ResultVo resultVo = null;
        if (searchResult.getCheckBrandList().size() == 0 && searchResult.getCheckSpecList().size() == 0) {
            System.out.println("没有收到选中的规格，将返回关键字查询的数据!!!");
            resultVo = new ResultVo(1002,searchResult.getKeyword());
        }else {
            CustomPage<Goods> page = goodsService.screenSearch(searchResult);
            searchResult.setPage(page);
            resultVo = page != null ? new ResultVo(200,searchResult) :
                    new ResultVo(1000,"您查询的规格不存在！",searchResult);
        }
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
